package com.globalwave.common
{

	//public class FilterComboBox extends ComboBox
	
	import com.globalwave.util.U;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ICollectionView;
	import mx.collections.IViewCursor;
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.controls.ComboBox;
	import mx.events.FlexEvent;
	import mx.utils.StringUtil;
	
	/**  
	 * ComboBox过滤自动完成组件  
	 * @author 韩敬诺   
	 *   
	 */  
	public class FilterComboBox extends ComboBox   
	{   
		public function FilterComboBox()   
		{   
			if(!width){   
				//如果没有指定width则设置默认的宽度   
				width=120;   
			}   
			setStyle("paddingLeft","0");   
			selectedIndex=-1;   
			editable=true;   
			addEventListener("creationComplete",init);   
		}   
		private var ac:ICollectionView;//保存数据源   
		
		/**  
		 *   
		 * @param event  
		 *   
		 */  
		private function init(event:Event):void{   
			//ac=dataProvider as ArrayCollection;
			ac = super.collection ;
		}   
		override public function set dataProvider(value:Object):void
		{
			super.dataProvider = value ;
			if (ac == null) {
			    ac = super.collection ;
			}
			
		}
		/**  
		 * 重写下三角按钮的点击事件  
		 * @param event  
		 *   
		 */  
		override protected function downArrowButton_buttonDownHandler(event:FlexEvent):void{   
			dataProvider=ac;   
			super.downArrowButton_buttonDownHandler(event);   
		}   
		/**  
		 * 当输入框中的值发生变化时开始过滤  
		 * @param event  
		 *   
		 */  
		override protected function textInput_changeHandler(event:Event):void{   
			super.textInput_changeHandler(event); 
			//当数据源不为空并且关键字不为空时开始搜索数据源   
			if(ac){   
				var keyWord:String=StringUtil.trim(textInput.text);   
				if(keyWord){   
					searchKeyWord(keyWord);    
				}   
			}   
		}   
		/**  
		 *   
		 * @param keyWord关键字  
		 *   
		 */  
		private function searchKeyWord(keyWord:String):void{   
			var dp:ICollectionView=filterData(ac);   
			//当数据源改变的时候   
			if(dp.length>0){   
				dataProvider=dp;   
				dropdown.selectedIndex=-1;   
				dropdown.verticalScrollPosition=0;   
				textInput.text = keyWord ;
				textInput.setFocus();   
				//注意先设置关键字被选中后打开下拉列表   
				textInput.selectRange(keyWord.length,keyWord.length);   
				open();   
			}else{   
				dataProvider=ac;   
				textInput.text="";   
				selectedIndex=-1;   
				close();   
			}   
		}   
		/**  
		 *   
		 * @param item  
		 * @return   
		 *   
		 */  
		private function filterData(ac:ICollectionView):ICollectionView{   
			var dp:ArrayCollection=new ArrayCollection();   
			var cursor:IViewCursor = ac.createCursor() ;
			
			while(!cursor.afterLast) {
				var item:Object=cursor.current;   
				var value:String=item[labelField];   
				if(value.indexOf(textInput.text)!=-1){   
					dp.addItem(item);   
				} 
				cursor.moveNext() ;
			}
			/*
			for(var i:int=0;i<ac.length;i++){   
				var item:Object=ac.getItemAt(i);   
				if(item.hasOwnProperty(labelField)){   
					var value:String=item[labelField];   
					if(value.indexOf(textInput.text)!=-1){   
						dp.addItem(item);   
					}   
				}   
			} */  
			return dp;   
		}   
		
		public var valueField:String
		
		public function set selectedValue(value:String):void {	
			
			var valueString:String = U.obj2String(value) ;
			
			var datas:XMLListCollection = (this.dataProvider as XMLListCollection) ;
			
			var property:String = this.valueField == null? "id": this.valueField ;
			
			for(var i:int = 0; i < datas.length; i++)
			{
				var propertyValue:String = U.obj2String(datas.getItemAt(i)[property]) ;
				if(propertyValue == valueString)
				{
					this.selectedIndex = i ;
					return ;
				}
			}
			this.selectedIndex = -1 ;
		}
		
		public function get selectedValue():String {
		    var selectObj:Object = this.selectedItem ;
			
			if (selectObj == null) {
			    return null ;
			}
			
			var property:String = this.valueField == null? "id": this.valueField ;
			
			return U.obj2String(selectObj[property]) ;
		}
	}   

}